package com.smarth.zhizulian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.smarth.zhizulian.app.BaseActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.R;
import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.CommonUtils;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.view.TitleBar;

import org.simple.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * Created by 18865 on 2017/12/26.
 */

public class EditActivity extends BaseActivity {
    public static final String INTENT_MODE = "INTENT_MODE";
    public static final String INTENT_TITLE = "INTENT_TITLE";
    public static final String INTENT_TEXT = "INTENT_TEXT";
    public static final int MODE_NAME = 0;
    public static final int MODE_WECHAT = 1;
    @BindView(R.id.et_edit)
    EditText etEdit;
    private int mMode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        ButterKnife.bind(this);
        setTitlebar(getIntent().getStringExtra(INTENT_TITLE), true, "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtils.hideKeyBoard(mContext, etEdit);
                fetchEdit();
            }
        });
        mMode = getIntent().getIntExtra(INTENT_MODE, 0);
        etEdit.setText(getIntent().getStringExtra(INTENT_TEXT));
    }

    public static void open(Context context, String title, int mode, String text) {
        Intent i = new Intent(context, EditActivity.class);
        i.putExtra(INTENT_MODE, mode);
        i.putExtra(INTENT_TITLE, title);
        i.putExtra(INTENT_TEXT, text);
        context.startActivity(i);
    }

    private void fetchEdit() {
        Map<String, Object> map = new HashMap<>();
        switch (mMode) {
            case MODE_NAME:
                map.put("nickname", etEdit.getText().toString());
                break;
            case MODE_WECHAT:
                map.put("wechat", etEdit.getText().toString());
                break;
        }
        KumaHttp.getService().mineEditUserInfo(map)
                .compose(RxCompos.getBEAllLoginCompos())
                .subscribe(new ProgressObserver<BaseEntity<Object>>(mContext, true) {
                    @Override
                    public void onNext(BaseEntity<Object> s) {
                        EventBus.getDefault().post(new EventBean(), BaseConfig.EventTag.ON_EDIT);
                        EditActivity.this.finish();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        if (TextUtils.isEmpty(etEdit.getText().toString())) {
                            KumaToast.show(mContext, "请输入内容");
                            d.dispose();
                            return;
                        }
                        super.onSubscribe(d);
                    }
                });
    }

}
